import itertools
import typing
from typing import Union, Optional, TypeVar
from urllib.request import Request
from uuid import UUID

import pydantic
from pydantic import fields, Field, validator, PydanticValueError
from pydantic.fields import ModelField
from pydantic.utils import Representation

from afast_core.core_db.sqlalchemy.schema import BaseSchema
from fastapi import Query, Depends, Path, Body

from pydantic import ValidationError


class SchemaBase(BaseSchema):
    pass










from pydantic import BaseModel

MODEL_SCHEMA = TypeVar("MODEL_SCHEMA", bound=SchemaBase)


#   # # lt：less than 小于
#         #         # le：less than or equal to 小于等于
#         #         # eq：equal to 等于
#         #         # ne：not equal to 不等于
#         #         # ge：greater than or equal to 大于等于
#         #         # gt：greater than 大于
#         # ===========================
#         #     __field_constraints__ = {
#         #         'min_length': None,
#         #         'max_length': None,
#         #         'regex': None,
#         #         'gt': None,
#         #         'lt': None,
#         #         'ge': None,
#         #         'le': None,
#         #         'multiple_of': None,
#         #         'allow_inf_nan': None,
#         #         'max_digits': None,
#         #         'decimal_places': None,
#         #         'min_items': None,
#         #         'max_items': None,
#         #         'unique_items': None,
#         #         'allow_mutation': True,
#         #     }
#         #    'value_error.jsondecode'：此类的异常会导致我们的装饰器注入依赖导致转换异常
#         # 其他错误类型：type_error.float

# class BaseValidator(BaseModel):
#     def __call__(self, value: typing.Any, field: ModelField) -> typing.Any:
#         return NotImplemented


# class BaseField(Representation):
#     _validators: typing.List[BaseValidator] = []
#     @classmethod
#     def __get_validators__(cls) -> typing.Iterable[BaseValidator]:
#         return itertools.chain(cls._validators)


# class MyField(BaseField):
#     def __init__(self, *args, optional=True, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.optional = optional

class NotABarError(PydanticValueError):
    # 错误类型编码定义
    code = 'not_none_ok'
    # 错误描述信息--其中wrong_value对应的是检测的字段信息
    # msg_template = 'value is not "bar", got "{wrong_value}"'
    msg_template = '字段信息不能为空'


class BaseValidator(BaseModel):
    pass


class Length(BaseValidator):
    min: int = -1

    @validator("min")
    def min_or_max(cls, v: int, values: typing.Any) -> int:
        print("==================111111111111", v, values)
        return v

    def __call__(self, value: typing.Any, field: ModelField) -> typing.Any:
        length = value and len(value) or 0
        print("dddddddddddddddddddddddd")


from pydantic import errors


class UserModel(SchemaBase):
    # 16. preview：预览，用于预览字段的值。
    name: str = Field(..., title="姓名", validator=validator)
    age: int = Field(..., title="年龄", gt=50, le=150)
    sex: str = 'male'
    address: str = None
    mobile: str = None

    # def __call__(self, value: typing.Any, field: ModelField) -> typing.Any:
    #     length = value and len(value) or 0
    #     print("dddddddddddddddddddddddd")

    # @validator('name')
    # def name_must_contain_space(cls, v):
    #     if not v:
    #         raise NotABarError(wrong_value="name")
    #     return v


class UserModelPath(SchemaBase):
    # 如果包含的字段信息和路径参数值是一样的话，它也会自动转化为路径参数不需要手动声明 Path(..., title='路径参数')或Query(..., title='查询参数')
    name: str = Path(..., title='路径参数')
    name2222: str = Query(..., title='查询参数')
